home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Tree View and List View / ImageDirectory / ImageDirectory.cs next >
Encoding:
Text File  |  2001-01-15  |  4.0 KB  |  131 lines

  1. //---------------------------------------------
  2. // ImageDirectory.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using Petzold.ProgrammingWindowsWithCSharp;
  5. using System;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8.  
  9. class ImageDirectory: Form
  10. {
  11.      PictureBoxPlus    picbox;
  12.      DirectoryTreeView dirtree;
  13.      ImagePanel        imgpanel;
  14.      Splitter          split;
  15.      TreeNode          tnSelect;
  16.      Control           cntlClicked;
  17.      Point             ptPanelAutoScroll;
  18.  
  19.      public static void Main()
  20.      {
  21.           Application.Run(new ImageDirectory());
  22.      }
  23.      public ImageDirectory()
  24.      {
  25.           Text = "Image Directory";
  26.           BackColor = SystemColors.Window;
  27.           ForeColor = SystemColors.WindowText;
  28.  
  29.                // Create (invisible) control for displaying large image.
  30.  
  31.           picbox            = new PictureBoxPlus();
  32.           picbox.Parent     = this;
  33.           picbox.Visible    = false;
  34.           picbox.Dock       = DockStyle.Fill;
  35.           picbox.SizeMode   = PictureBoxSizeMode.StretchImage;
  36.           picbox.NoDistort  = true;
  37.           picbox.MouseDown += new MouseEventHandler(PictureBoxOnMouseDown);
  38.  
  39.                // Create controls for displaying thumbnails.
  40.  
  41.           imgpanel        = new ImagePanel();
  42.           imgpanel.Parent = this;
  43.           imgpanel.Dock   = DockStyle.Fill;
  44.           imgpanel.ImageClicked += 
  45.                          new EventHandler(ImagePanelOnImageClicked);
  46.  
  47.           split           = new Splitter();
  48.           split.Parent    = this;
  49.           split.Dock      = DockStyle.Left;
  50.           split.BackColor = SystemColors.Control;
  51.  
  52.           dirtree        = new DirectoryTreeView();
  53.           dirtree.Parent = this;
  54.           dirtree.Dock   = DockStyle.Left;
  55.           dirtree.AfterSelect += 
  56.                     new TreeViewEventHandler(DirectoryTreeViewOnAfterSelect);
  57.  
  58.                // Create menu with one item (Refresh).
  59.  
  60.           Menu = new MainMenu();
  61.           Menu.MenuItems.Add("&View");
  62.           MenuItem mi = new MenuItem("&Refresh", 
  63.                               new EventHandler(MenuOnRefresh), Shortcut.F5);
  64.           Menu.MenuItems[0].MenuItems.Add(mi);
  65.      }
  66.      void DirectoryTreeViewOnAfterSelect(object obj, TreeViewEventArgs tvea)
  67.      {
  68.           tnSelect = tvea.Node;
  69.           imgpanel.ShowImages(tnSelect.FullPath);
  70.      }
  71.      void MenuOnRefresh(object obj, EventArgs ea)
  72.      {
  73.           dirtree.RefreshTree();
  74.      }
  75.      void ImagePanelOnImageClicked(object obj, EventArgs ea)
  76.      {
  77.                // Get clicked control and image.
  78.           
  79.           cntlClicked = imgpanel.ClickedControl;
  80.           picbox.Image = imgpanel.ClickedImage;
  81.  
  82.                // Save auto-scroll position.
  83.  
  84.           ptPanelAutoScroll = imgpanel.AutoScrollPosition;
  85.           ptPanelAutoScroll.X *= -1;
  86.           ptPanelAutoScroll.Y *= -1;
  87.  
  88.                // Hide and disable the normal controls.
  89.  
  90.           imgpanel.Visible = false;
  91.           imgpanel.Enabled = false;
  92.           imgpanel.AutoScrollPosition = Point.Empty;
  93.  
  94.           split.Visible = false;
  95.           split.Enabled = false;
  96.  
  97.           dirtree.Visible = false;
  98.           dirtree.Enabled = false;
  99.  
  100.                // Make the picture box visible.
  101.  
  102.           picbox.Visible = true;
  103.      }
  104.           // Event handlers and method involved with restoring controls
  105.  
  106.      void PictureBoxOnMouseDown(object obj, MouseEventArgs mea)
  107.      {
  108.           RestoreControls();
  109.      }
  110.      protected override void OnKeyDown(KeyEventArgs kea)
  111.      {
  112.           if (kea.KeyCode == Keys.Escape)
  113.                RestoreControls();
  114.      }
  115.      void RestoreControls()
  116.      {
  117.           picbox.Visible = false;
  118.  
  119.           dirtree.Visible = true;
  120.           dirtree.Enabled = true;
  121.  
  122.           split.Enabled = true;
  123.           split.Visible = true;
  124.  
  125.           imgpanel.AutoScrollPosition = ptPanelAutoScroll;
  126.           imgpanel.Visible = true;
  127.           imgpanel.Enabled = true;
  128.           cntlClicked.Focus();
  129.      }
  130. }
  131.